package com.rainyun.rc.orm;

import com.rainyun.rc.common.VersionUtil;
import com.rainyun.rc.util.RainStr;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class MysqlDB extends CommonDB{
	
	String username;
	String location;
	String password;
	String port;
	String database;
	String params = "?useUnicode=true&characterEncoding=UTF-8";
	
	private String all_url;
	
	/**核心orm的构造器*/
	public MysqlDB(String username,String password, String location,String port, String database, String params) {
		super();
		this.username = username;
		this.location = location;
		this.password = password;
		this.port = port;
		this.database = database;
		this.params = params;
	}
	@Override
	public RainSql newSql(Class<?> clazz) {
		return new MysqlSql(clazz,this);
	}
	
	public String createAllUrl() {
		return String.format("jdbc:mysql://%s:%s/%s%s",
				location,port,database,params);
	}

	public String getUsername() {
		return username;
	}

	public MysqlDB setUsername(String username) {
		this.username = username;
		return this;
	}

	public String getLocation() {
		return location;
	}

	public MysqlDB setLocation(String location) {
		this.location = location;
		return this;
	}

	public String getPassword() {
		return password;
	}

	public MysqlDB setPassword(String password) {
		this.password = password;
		return this;
	}

	public String getPort() {
		return port;
	}

	public MysqlDB setPort(String port) {
		this.port = port;
		return this;
	}
	public String getDatabase() {
		return database;
	}

	public MysqlDB setDatabase(String database) {
		this.database = database;
		return this;
	}

	public String getParams() {
		return params;
	}

	public MysqlDB setParams(String params) {
		this.params = params;
		return this;
	}

	@Override
	public boolean connect() throws SQLException{
		try {
			if(all_url==null)all_url = createAllUrl();
			Class.forName("java.sql.DriverManager");
			Connection con = connection;
			if(con!=null)//关掉原来连接
				con.close();
			connection = DriverManager.getConnection(all_url,username,password);
			return true;
		} catch (ClassNotFoundException e) {
			handleError(null,e);
		} catch (SQLException e) {
			handleError(null,e);
			throw e;
		}
		return false;
	}

	@Override
	public void setDataVersion(Class<?> c){
		String com = getComment(c);
		String ver = VersionUtil.getVersion(c);
		if(com==null&&ver==null)return;
		com=com!=null?com:"";
		String[] a = com.split(">>>",2);
		com = a[0];
		if(ver!=null)com+=">>>"+ver;
		try {
			sqlUpdate("alter table "+getClazzInfo(c).getPath()+" comment '"+com+"';");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String getDataVersion(Class<?> c){
		String re = getComment(c);
		if(re!=null){
			String com = re.toString();
			String[] a = com.split(">>>",2);
			if(a.length>1){
				String ver = a[1].trim();
				return ver;
			}
		}
		return null;
	}
	/***/
	public String getComment(Class<?> c){
		try {
			MysqlSql sql = (MysqlSql) newSql(null);
			sql.SqlParts.put(CommonSql.SqlTypes.select,new SqlPart("TABLE_COMMENT"));
			sql.SqlParts.put(CommonSql.SqlTypes.from,new SqlPart("TABLES"));
			sql.where("TABLE_SCHEMA",database);
			sql.where("TABLE_NAME",getClazzInfo(c).getPath());
			Object re = sql.getResult();
			return re!=null?re.toString():null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}


}




